package com.song.pj.module.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.song.pj.module.pojo.ResponseResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

//@Controller
//@ResponseBody
@RestController//==@Controller+@ResponseBody
public class JsonObjectController {
    @RequestMapping("/doConvertResponseToJson")
//    @ResponseBody
    public  ResponseResult doConvertResponseToJson(){
        ResponseResult r = new ResponseResult();
        r.setCode(200);
        r.setMessage("update ok");
        return  r;
    }

    @RequestMapping("/doPrintJson")
//    @ResponseBody
    public  void doPrintJson(HttpServletResponse response) throws IOException {
        Map<String,Object> map = new HashMap();
        map.put("username", "刘德华");
        map.put("state",true);
        String jsonStr = new ObjectMapper().writeValueAsString(map);
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;cahrset=utf-8");
        response.getWriter().println(jsonStr);
    }

    @RequestMapping("doTest")
    public Map<String,Object> doTest(){
        Map<String,Object> map = new HashMap();
        map.put("username", "刘德华");
        map.put("state",true);
        return map;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          }
}
